// pages/order/list/list.js
const fetch = require('../../../utils/fetch.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    is_last: false
  },
  last_id: 0,
  loadData: function(data, succse, fail) {
    data.row = 10
    fetch('food/orderlist', data).then(res => {
      this.last_id = res.data.last_id
      this.setData({
        is_last: res.data.is_last
      }, () => {
        succse(res.data)
      })
    }, fail)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中...'
    })
    this.loadData({
      last_id: 0
    }, (data) => {
      this.setData({
        orderList: data.list
      }, () => {
        wx.hideLoading()
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var app = getApp()
    if (app.isReloadOrderList) {
      this.onLoad()
      app.isReloadOrderList = false
    }
  },
  orderdetail: function(e) {
    var index = e.currentTarget.dataset.postId
    wx.navigateTo({
      url: '../detail/detail?order_id=' + index
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    wx.showNavigationBarLoading()
    that.loadData({
      last_id: 0
    }, data => {
      setTimeout(function() {
        that.setData({
          orderList: data.list
        })
        wx.hideNavigationBarLoading()
      }, 1500)
    }, () => {
      wx.hideNavigationBarLoading()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (that.data.is_last) {
      return
    }
    wx.showLoading({
      title: '玩命加载中...'
    })
    setTimeout(function() {
      that.loadData({
        last_id: this.last_id
      }, (data) => {
        var orderList = that.data.orderList
        for (var i = 9; i < data.list.length; i++) {
          orderList.push(data.list[i])
        }
        that.setData({
          orderList: orderList
        }, () => {
          wx.hideLoading()
        })
      })
    }, 0)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})